﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QDT.Common;
using QDT.Core.Domain;

namespace QDT.Core.Orders
{
    /// <summary>
    /// 充值订单
    /// </summary>
    public class ChargeOrder:Entity
    {

        public ChargeOrder(){}

        public ChargeOrder(int userid,string orderSn,decimal money) {
            Check.Argument.IsNotNegativeOrZero(money,"money");

            UserID = userid;
            OrderSN = orderSn;
            TotalPrice = money;

            CreateTime = DateTime.Now;
            UpdateTime = CreateTime;

            Status=ChargeOrderStatus.Wait;
            Logs=new List<ChargeOrderLog>();
        }

        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderSN { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }

        public int InnerStatus { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public ChargeOrderStatus Status
        {
            get
            {
                return (ChargeOrderStatus)InnerStatus;
            }
            set
            {
                InnerStatus = (int)value;
            }
        }

        /// <summary>
        /// 充值总额
        /// </summary>
        public decimal TotalPrice { get; set; }

        /// <summary>
        /// 支付总额
        /// </summary>
        public decimal PayAmount { get; set; }

        /// <summary>
        /// 支付方式ID
        /// </summary>
        public string PaymentKey { get; set; }

        /// <summary>
        /// 银行码
        /// </summary>
        public string BankCode { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDelete { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Memo { get; set; }

        /// <summary>
        /// 管理员备注
        /// </summary>
        public string AdminMemo { get; set; }

        /// <summary>
        /// 充值订单日志集合
        /// </summary>
        public virtual ICollection<ChargeOrderLog> Logs { get; set; }


        public void Add(ChargeOrderLog log)
        {
            this.Logs.Add(log);
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="optor">操作人</param>
        /// <param name="info">处理信息</param>
        public void Add(string optor, string info)
        {
            this.Add(new ChargeOrderLog { Time=DateTime.Now,Operator=optor,Info=info});
        }

        /// <summary>
        /// 管理员收款
        /// </summary>
        /// <param name="admin">管理员帐号</param>
        /// <param name="memo">管理员备注</param>
        public void AdminProcess(string admin,string memo) {

            this.Status = ChargeOrderStatus.Completed;
            this.PayAmount = TotalPrice;
            this.AdminMemo = memo;

            Add(admin,"管理员收款");
        }
    }
}
